package com.fh.controller.cuijimanage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.fh.util.R;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.model.Page;
import com.fh.model.system.Role;
import com.fh.model.system.User;
import com.fh.service.assign_out.IAssignOutService;
import com.fh.service.cuijimanage.CuijimanageServiceImpl;
import com.fh.service.system.organization.OrganizationServiceImpl;
import com.fh.service.system.user.UserService;
import com.fh.util.Const;
import com.fh.util.PageData;
import com.fh.util.UserUtils;

@Controller
@RequestMapping(value = "/cuijiMessage")
public class CuijiMessageController extends BaseController {

	private Logger logger = LoggerFactory
			.getLogger(CuijiMessageController.class);
	@Autowired
	private CuijimanageServiceImpl cuijimanageServiceImpl;

	@Autowired
	private OrganizationServiceImpl organizationService;

	@Autowired
	private UserService userService;
	@Autowired
	private IAssignOutService assignOutService;

	/**
	 * 至催计消息页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/tocuijiMessage.do")
	public ModelAndView toCuijiMessage(HttpServletRequest request, Page page) {
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		//获取页面参数去空并射入page里面
		PageData pageData = assignOutService.setPdWithTrim(this.getPageData());
		page.setPd(UserUtils.setDataRole(pd, userService));
		String status = pd.getString("status");
		String temp = pd.getString("temp");
		String temp1 = pd.getString("temp1");
		if (StringUtils.isBlank(status)) {
			status = null;
			pd.put("status", status);
		}
		String currentPage = pd.getString("page.currentPage") == null ? "1"
				: pd.getString("page.currentPage");
		String showCount = pd.getString("page.showCount") == null ? "10"
				: pd.getString("page.showCount");
		page.setCurrentPage(Integer.parseInt(currentPage));
		page.setShowCount(Integer.parseInt(showCount));
		if (StringUtils.isBlank(status) && StringUtils.isBlank(temp) && StringUtils.isBlank(temp1)) {
			//设置pageList为空
			page.setPageList(new ArrayList<>());
		}else{
			page = (Page) cuijimanageServiceImpl.cuijiPage(page);
		}
		mv.setViewName("cuijimanage/cuijiMessage");
		mv.addObject("page", page);
		mv.addObject("pd", pd);
		return mv;

	}

	/**
	 * 催记消息未读消息查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/selectMessageNum.do")
	@ResponseBody
	public Map<String, Object> selectMessage(HttpServletRequest request,
			Page page) {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = UserUtils.getUser();
		Role role = user.getRole();
		List<User> list1 = new ArrayList<>();
		int count = 0;
		if (Const.ROLE_CSZZ.equals(role.getROLE_ID())) {
			// 组长 获取组长所有下属组员信息(包括组长自己的)
			try {
				list1 = userService.getChildrenUser(user.getUSER_ID());
				list1.add(user);
				Integer[] ids = new Integer[list1.size()];
				if (list1 != null && list1.size() > 0) {
					for (int i = 0; i < list1.size(); i++) {
						int id = list1.get(i).getUSER_ID();
						map.put("ids", id);
						int result = cuijimanageServiceImpl.msgCount(map);
						count = result + count;
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (Const.ROLE_CSY.equals(role.getROLE_ID())) {
			int id = user.getUSER_ID();
			map.put("ids", id);
			count = cuijimanageServiceImpl.msgCount(map);
		} else if ("1".equals(role.getROLE_ID())) {
			map.put("ids", null);
			count = cuijimanageServiceImpl.msgCount(map);
		}
		map.put("result", count);
		return map;
	}

	/**
	 * 标记未读为已读
	 * 
	 * @param request
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/changeWeidu.do")
	public ModelAndView weiduListPage(HttpServletRequest request, int id,
			Page page) {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		int msgStatus = cuijimanageServiceImpl.updateWeiduStatus(map);
		// mv.setViewName("redirect:cuijimanage/cuijiMessage");
		if (msgStatus == 0) {
			logger.info("更改状态失败");
			mv.setViewName("cuijimanage/doError");
		}
		
		PageData pd =this.getPageData();
		String currentPage = pd.getString("page.currentPage") == null ? "1"
				: pd.getString("page.currentPage");
		String showCount = pd.getString("page.showCount") == null ? "10"
				: pd.getString("page.showCount");
		page.setCurrentPage(Integer.parseInt(currentPage));
		page.setShowCount(Integer.parseInt(showCount));
		page = (Page) cuijimanageServiceImpl.cuijiPage(page);
		mv.setViewName("cuijimanage/cuijiMessage");
		mv.addObject("page", page);
		return mv;
	}

	/**
	 * 删除操作
	 * 
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteMsg.do")
	public ModelAndView cuijiMsgDelete(HttpServletRequest request, int id,
			Page page) {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		int msgDelete = cuijimanageServiceImpl.cuijiMsgDelete(map);
		// mv.setViewName("cuijimanage/cuijiMessage");
		if (msgDelete == 0) {
			logger.info("删除操作失败");
			mv.setViewName("cuijimanage/doError");
		}
		PageData pd =this.getPageData();
		String currentPage = pd.getString("page.currentPage") == null ? "1"
				: pd.getString("page.currentPage");
		String showCount = pd.getString("page.showCount") == null ? "10"
				: pd.getString("page.showCount");
		page.setCurrentPage(Integer.parseInt(currentPage));
		page.setShowCount(Integer.parseInt(showCount));
		page = (Page) cuijimanageServiceImpl.cuijiPage(page);
		mv.setViewName("cuijimanage/cuijiMessage");
		mv.addObject("page", page);
		return mv;
	}

	/**
	 * 催记消息未读消息查询列表展示
	 * 
	 * @param request
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/selectWeiduMsgList.do")
	public ModelAndView cuijiWeiduListPage(HttpServletRequest request, Page page) {
		ModelAndView mv = new ModelAndView();
		PageData pd =this.getPageData();
		String currentPage = pd.getString("page.currentPage") == null ? "1"
				: pd.getString("page.currentPage");
		String showCount = pd.getString("page.showCount") == null ? "10"
				: pd.getString("page.showCount");
		page.setCurrentPage(Integer.parseInt(currentPage));
		page.setShowCount(Integer.parseInt(showCount));
		page = (Page) cuijimanageServiceImpl.cuijiPage(page);
		mv.setViewName("cuijimanage/cuijiMessage");
		mv.addObject("page", page);
		return mv;
	}
}
